﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.Catalog
{
    public partial class frmDoituong : PRE.Catalog.FrmBase
    {
        DAL.Entities_DAL.DOITUONG_DAL _dal = new DAL.Entities_DAL.DOITUONG_DAL();

        public frmDoituong()
        {
            InitializeComponent();
        }

      

        /// <summary>
        /// Perform when click add button
        /// </summary>
        /// 

        //them
        protected override void PerformAdd()
        {
                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);
                    ResetText();
                    txtMSSV.Focus();
              
            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {

            //click zo test de xoa
            string id = txtMSSV.Text;

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txtLH.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            txtMSSV.Properties.ReadOnly = true;

            if (_dtb != null)
            {
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            base.PerformRefresh();
        }
        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            var ten = Proper(txtLH.Text);
            var cv = Proper(txtCV.Text);

            if (IsAdd)
            {
                var o = new DAL.Entities.DOI_TUONG
                {
                    MSSV = txtMSSV.Text,
                    LOP_HOC = ten,
                    C_VU = cv,
                };

                //them
                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            //sua
            if (IsEdit)
            {
                var _id = grvMain.GetFocusedRowCellValue("MA_NCC") + "";

                var o = new DAL.Entities.DOI_TUONG
                {
                    MSSV = txtMSSV.Text,
                    LOP_HOC = ten,
                    C_VU = cv,
                };
                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            base.PerformSave();
        }
        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            //bam nut them lam rong texbox
            txtMSSV.Text = null;
            txtLH.Text = null;
            txtCV.Text = null;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtMSSV.DataBindings.Clear();
            txtLH.DataBindings.Clear();
            txtCV.DataBindings.Clear();
            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            //show len texbox
            txtMSSV.DataBindings.Add("EditValue", _dtb, ".MSSV");
            txtLH.DataBindings.Add("EditValue", _dtb, ".LOP_HOC");
            txtCV.DataBindings.Add("EditValue", _dtb, ".C_VU");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //show len cho xem
            txtMSSV.Properties.ReadOnly = isReadOnly;
            txtLH.Properties.ReadOnly = isReadOnly;
            txtCV.Properties.ReadOnly = isReadOnly;

            dsDT.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }
        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            //load len luoi data
            _dtb = _dal.Select();

            if (_dtb != null)
            {
                dsDT.DataSource = _dtb;

                //dem so nhan vien duoi lable
                string _dem = _dal.Select_count();
                lbNhanvien.Text = _dem.ToString() + " Sinh Viên";
            }

            base.LoadData();
        }
    }
}
